home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Game / think / aGibbet.lha / aGibbet / gibbet152me.c < prev   
C/C++ Source or Header  |  2004-08-07  |  14KB  |  548 lines

  1. /*
  2. ** gibbet.c
  3. **
  4. ** (C) Copyright 2004 by Imax.
  5. **  All other rights reserved.
  6. ** This information is provided "as is"; no warranties are made.  All
  7. ** use is at your own risk. No liability or responsibility is assumed.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <exec/types.h>
  13. #include <exec/memory.h>
  14. #include <dos/dos.h>
  15. #include <intuition/intuition.h>
  16. #include <intuition/gadgetclass.h>
  17. #include <intuition/icclass.h>
  18. #include <intuition/classes.h>
  19. #include <libraries/gadtools.h>
  20. #include <graphics/gfx.h>
  21. #include <libraries/dos.h>
  22. #include <time.h>
  23. #include <datatypes/datatypes.h>
  24. #include <datatypes/pictureclass.h>
  25. #include <datatypes/pictureclass.h>
  26. #include <utility/tagitem.h>
  27. #include <workbench/icon.h>
  28. #include <workbench/startup.h>
  29. #include <workbench/workbench.h>
  30.  
  31. #include <clib/dos_protos.h>
  32. #include <clib/wb_protos.h>
  33.  
  34. #include <proto/exec.h>
  35. #include <proto/graphics.h>
  36. #include <proto/diskfont.h>
  37. #include <proto/intuition.h>
  38. #include <proto/gadtools.h>
  39. #include <proto/dos.h>
  40. #include <proto/icon.h>
  41. #include <proto/datatypes.h>
  42. #include <proto/utility.h>
  43. #include <proto/wb.h>
  44.  
  45. /*------------------------------------------------------------------------*/
  46. char *ID = "$VER: aGibbet v1.52me (07.08.04)\r\n";
  47.  
  48.  
  49. int main(int argc, char **argv);
  50. void bail_out(int, STRPTR);
  51. struct Gadget *CreateAllGadgets(struct Gadget **, void *, UWORD, APTR);
  52. struct TextAttr Gibbet15 =
  53. {
  54.     "Gibbet.font",  /* Name */
  55.     15,         /* YSize */
  56.     0,          /* Style */
  57.     0,          /* Flags */
  58. };
  59.  
  60. extern struct Library *SysBase;
  61. struct GfxBase *GfxBase = NULL;
  62. struct IntuitionBase *IntuitionBase = NULL;
  63. struct Library *DiskfontBase = NULL;
  64. struct Library *GadToolsBase = NULL;
  65. struct Library *DataTypesBase = NULL;
  66. struct Library *UtilityBase = NULL;
  67. struct Library *IconBase = NULL;
  68. struct TextFont *font = NULL;
  69. struct Screen *mysc = NULL;
  70. struct Gadget *glist = NULL;
  71. struct Window *mywin = NULL;
  72. void *vi = NULL;
  73.  
  74. BOOL terminated = FALSE;
  75.  
  76.     Object *dtobj;
  77.     BPTR fileimg,filewords;
  78.     UBYTE imgfilename[]="PROGDIR:pics/gibbet0.pic";
  79.     ULONG lengthimg,lengthwords;
  80.     ULONG sigr;
  81.     ULONG cntwords, cnt1;
  82.     ULONG rndword;
  83.     APTR imagearray, wordsarray;
  84.     UBYTE *addr1, *addr2;
  85.     UWORD wrongcnt;
  86.     UBYTE selword[21], guidword[21];
  87.     struct Gadget *fistgad;
  88.     UBYTE textwin[]="WINNER! -->";
  89.     UBYTE textgib[]="HANGING! -->";
  90.     UBYTE textsp[]="            ";
  91.     UBYTE my_text[]="                    ";
  92.  
  93. struct IntuiText my_intui_text=
  94. {
  95.   1,         /* FrontPen, colour register 1. */
  96.   0,         /* BackPen, colour register 2. */
  97.   JAM2,      /* DrawMode, draw the characters with colour 1, on a colour */
  98.              /* 2 background. (White text on a black background) */
  99.   0, 0,     /* LeftEdge, TopEdge. */
  100.   &Gibbet15,      /* ITextFont, use default font. */
  101.   my_text,   /* IText, the text that will be printed. */
  102.              /* (Remember my_text = &my_text[0].) */
  103.   NULL,      /* NextText, no other IntuiText structures are connected. */
  104. };
  105.     struct RDArgs *rdargs;
  106.     struct DiskObject *dob=NULL;
  107.     BOOL flagnb=FALSE;
  108.     #define TEMPLATE    "NAME/A"
  109.     #define OPT_NAME    0
  110.     #define NUM_OPTS    1
  111.     BOOL FromWb;
  112.  
  113. int main(int argc, char **argv)
  114. {
  115.     struct WBStartup *wbMsg = NULL;
  116.     struct IntuiMessage *imsg;
  117.     struct Gadget *gad, *crntgad;
  118.     ULONG imsgClass;
  119.     UWORD imsgCode;
  120.     UWORD topborder;
  121.     int i;
  122.     UBYTE gadgsel,koda,gadnum=0;
  123.     BOOL guidflag, ngflag;
  124.     ULONG options[NUM_OPTS]={0};
  125.     STRPTR namebw;
  126.  
  127.  
  128.     if (!(GfxBase = (struct GfxBase *)
  129.     OpenLibrary("graphics.library", 36L)))
  130.     bail_out(20, "Requires V36 graphics.library");
  131.  
  132.     if (!(IntuitionBase = (struct IntuitionBase *)
  133.     OpenLibrary("intuition.library", 36L)))
  134.     bail_out(20, "Requires V36 intuition.library");
  135.  
  136.     if (!(GadToolsBase = OpenLibrary("gadtools.library", 36L)))
  137.     bail_out(20, "Requires V36 gadtools.library");
  138.  
  139.     if (!(DataTypesBase = OpenLibrary ("datatypes.library", 39L)))
  140.     bail_out(20, "Requires V39 datatypes.library");
  141.  
  142.     if (!(UtilityBase = OpenLibrary ("utility.library", 39L)))
  143.     bail_out(20, "Requires V39 utility.library");
  144.  
  145.      if (!(DiskfontBase = (struct Library *)
  146.         OpenLibrary("diskfont.library", 37L) ))
  147.     bail_out(20, "Couldn't open Diskfont V37");
  148.  
  149.     if (!(font = OpenDiskFont(&Gibbet15)))
  150.     bail_out(20, "Failed to open Gibbet 15");
  151.  
  152.     if (!(mysc = LockPubScreen(NULL)))
  153.     bail_out(20, "Couldn't lock default public screen");
  154.  
  155.     if (!(vi = GetVisualInfo(mysc,
  156.     TAG_DONE)))
  157.     bail_out(20, "GetVisualInfo() failed");
  158.  
  159.     topborder = mysc->WBorTop + (mysc->Font->ta_YSize + 1);
  160.  
  161.     FromWb = (argc==0) ? TRUE : FALSE;
  162.  
  163.     if(!(IconBase = OpenLibrary("icon.library",33)))
  164.     bail_out(20, "Requires V33 icon.library");
  165.  
  166.     if(FromWb)
  167.     {  wbMsg = (struct WBStartup *)argv;
  168.        if (dob = GetDiskObject (wbMsg->sm_ArgList->wa_Name))
  169.        {namebw=FindToolType (dob->do_ToolTypes, "NAME");
  170.        }
  171.     }
  172.     else
  173.     {
  174.     rdargs = ReadArgs (TEMPLATE, (LONG *)options, NULL);
  175.     if (options[OPT_NAME]!=0) namebw = (STRPTR) options[OPT_NAME];
  176.     }
  177.  
  178.    filewords=Open(namebw, MODE_OLDFILE);
  179.  
  180.   if(filewords) {
  181.     Seek(filewords, 0, OFFSET_END);
  182.     lengthwords = Seek(filewords, 0, OFFSET_BEGINNING);
  183.     wordsarray = AllocMem(lengthwords, MEMF_ANY);
  184.     if(wordsarray) {
  185.       Read(filewords, wordsarray, lengthwords);
  186.       }
  187.     } else  bail_out(20, "Open Wordsbase failed");
  188.     Close(filewords);
  189.  
  190.     if (!CreateAllGadgets(&glist, vi, topborder, wordsarray))
  191.     {
  192.     bail_out(20, "CreateAllGadgets() failed");
  193.     }
  194.  
  195.     if (!(mywin = OpenWindowTags(NULL,
  196.     WA_Width, 326,
  197.     WA_InnerHeight, 172,
  198.     WA_Top,((mysc->Height)-172)>>2,
  199.     WA_Left,((mysc->Width)-326)>>1,
  200.     WA_Activate, TRUE,
  201.     WA_DragBar, TRUE,
  202.     WA_DepthGadget, TRUE,
  203.     WA_CloseGadget, TRUE,
  204.     WA_SizeGadget, FALSE,
  205.     WA_SimpleRefresh, TRUE,
  206.     WA_IDCMP, IDCMP_IDCMPUPDATE | CLOSEWINDOW | REFRESHWINDOW | BUTTONIDCMP,
  207.     WA_Title, "aGibbet",
  208.     WA_Gadgets, glist,
  209.     TAG_DONE)))
  210.     bail_out(20, "OpenWindow() failed");
  211.  
  212.     GT_RefreshWindow(mywin, NULL);
  213.  
  214.     srand (time(0));
  215.     addr1=(UBYTE*)wordsarray;
  216.     while (*addr1++!=0x0A)
  217.      gadnum++;
  218.  
  219. startnew:
  220.     wrongcnt=0;
  221.     imgfilename[19]=48;
  222.  
  223.     if (!(dtobj=NewDTObject(imgfilename,DTA_GroupID,GID_PICTURE,PDTA_Remap,TRUE,TAG_DONE)))
  224.     {   bail_out(20, "OpenDT failed");}
  225.  
  226.     SetDTAttrs (dtobj, NULL, NULL,
  227.                 GA_Left,12,
  228.                 GA_Top,topborder+12,
  229.                 GA_Width,80,
  230.                 GA_Height,150,
  231.                 ICA_TARGET, ICTARGET_IDCMP,
  232.                 TAG_DONE);
  233.  
  234.     AddDTObject (mywin, NULL, dtobj, -1);
  235.  
  236.     RefreshDTObjects (dtobj, mywin, NULL, NULL);
  237.  
  238.     ngflag=FALSE;
  239.     i=0;
  240.     addr1=my_text;
  241.     while(textsp[i]!=0)
  242.     (*addr1++)=textsp[i++];
  243.     *addr1=0;
  244.     PrintIText(mywin->RPort, &my_intui_text, 135, topborder+12);
  245.  
  246.     cntwords=1;
  247.     cnt1=lengthwords;
  248.     addr1=(UBYTE*)wordsarray;
  249.     while (cnt1>0)
  250.      {if (*addr1==0x0A) cntwords++;
  251.      addr1++;
  252.      cnt1--;
  253.      }
  254.     cntwords-=2;
  255.     rndword=rand() % cntwords;
  256.  
  257.     rndword++;
  258.     addr1=(UBYTE*)wordsarray;
  259.     while (rndword>0)
  260.      {if (*addr1==10) rndword--;
  261.       addr1++;
  262.      }
  263.     i=0;
  264.     while (*addr1!=10)
  265.      selword[i++]=(*addr1++);
  266.      selword[i]=0;
  267. //    printf("%s\n",selword);
  268.  
  269.     for(i=0;i<21;i++)
  270.     {if (selword[i]!=0) guidword[i]=42;
  271.      else guidword[i]=0;}
  272.  
  273.     i=0;
  274.     while (selword[i]!=0) i++;
  275.     i--;
  276.     rndword=rand() % i;
  277.     koda=selword[rndword];
  278.  
  279.     i=0;
  280.     while (selword[i]!=0)
  281.     {if (koda==selword[i]) guidword[i]=koda;
  282.      i++;}
  283.  
  284.     crntgad=fistgad;
  285.     koda=selword[rndword];
  286.     for (i=0;i<gadnum;i++)
  287.      {crntgad=crntgad->NextGadget;
  288.      if (koda==crntgad->GadgetID) {GT_SetGadgetAttrs(crntgad, mywin, NULL, GA_Disabled, TRUE, NULL); break;}
  289.      }
  290.  
  291.    i=0;
  292.    addr1=my_text;
  293.    while(guidword[i]!=0)
  294.    (*addr1++)=guidword[i++];
  295.    while(i<20)
  296.     {*addr1++=32; i++;}
  297.  
  298.    PrintIText(mywin->RPort, &my_intui_text, 125, topborder+44);
  299.  
  300.     while (!terminated)
  301.     {
  302.     sigr=Wait ((1 << mywin->UserPort->mp_SigBit) | SIGBREAKF_CTRL_C);
  303.    if (sigr & SIGBREAKF_CTRL_C) terminated = TRUE;
  304.  
  305.     while ((!terminated) && (imsg = GT_GetIMsg(mywin->UserPort)))
  306.     {
  307.         imsgClass = imsg->Class;
  308.         imsgCode = imsg->Code;
  309.  
  310.         gad = (struct Gadget *)imsg->IAddress;
  311.  
  312.         GT_ReplyIMsg(imsg);
  313.         switch (imsgClass)
  314.         {
  315.         case GADGETUP:
  316.  
  317.     gadgsel=(UBYTE)(gad->GadgetID);
  318.  
  319.        if (gadgsel==1)
  320.        {crntgad=fistgad;
  321.        for (i=0;i<gadnum;i++)
  322.         {crntgad=crntgad->NextGadget;
  323.         GT_SetGadgetAttrs(crntgad, mywin, NULL, GA_Disabled, FALSE, NULL);
  324.         }
  325.        RemoveDTObject (mywin,dtobj);
  326.        DisposeDTObject(dtobj);
  327.        GT_RefreshWindow(mywin, NULL);
  328.        goto startnew;
  329.        }
  330.  
  331.        if (ngflag==FALSE)
  332.       {
  333.        GT_SetGadgetAttrs(gad, mywin, NULL, GA_Disabled, TRUE, NULL);
  334.        GT_RefreshWindow(mywin, NULL);
  335.  
  336.        koda=gadgsel;
  337.        guidflag=FALSE;
  338.        i=0;
  339.        while (selword[i]!=0)
  340.        {if (selword[i]==koda)
  341.          {guidword[i]=koda;
  342.           guidflag=TRUE;}
  343.         i++;
  344.        }
  345.  
  346.        if (guidflag==FALSE)
  347.        { wrongcnt++;
  348.        RemoveDTObject (mywin,dtobj);
  349.        DisposeDTObject(dtobj);
  350.        imgfilename[19]=wrongcnt+48;
  351.     if (!(dtobj=NewDTObject(imgfilename,DTA_GroupID,GID_PICTURE,PDTA_Remap,TRUE,TAG_DONE)))
  352.     {   bail_out(20, "OpenDT failed");}
  353.  
  354.     SetDTAttrs (dtobj, NULL, NULL,
  355.                 GA_Left,12,
  356.                 GA_Top,topborder+12,
  357.                 GA_Width,80,
  358.                 GA_Height,150,
  359.                 ICA_TARGET, ICTARGET_IDCMP,
  360.                 TAG_DONE);
  361.  
  362.     AddDTObject (mywin, NULL, dtobj, -1);
  363.     RefreshDTObjects (dtobj, mywin, NULL, NULL);
  364.  
  365.         if(wrongcnt>6)
  366.         {i=0;
  367.         addr1=my_text;
  368.         while(selword[i]!=0)
  369.         (*addr1++)=selword[i++];
  370.         while((*addr1)!=0)
  371.         *addr1++=32;
  372.         PrintIText(mywin->RPort, &my_intui_text, 125, topborder+44);
  373.  
  374.         i=0;
  375.         addr1=my_text;
  376.         do
  377.         (*addr1++)=textgib[i++];
  378.         while(textgib[i]!=0);
  379.         *addr1=0;
  380.         PrintIText(mywin->RPort, &my_intui_text, 135, topborder+12);
  381.  
  382.         ngflag=TRUE;
  383.         }            //wrongcnt>6
  384.        } else       //guidflag=TRUE
  385.         {
  386.          i=0;
  387.          addr1=my_text;
  388.          while(guidword[i]!=0)
  389.          (*addr1++)=guidword[i++];
  390.          while((*addr1)!=0)
  391.          *addr1++=32;
  392.          PrintIText(mywin->RPort, &my_intui_text, 125, topborder+44);
  393.          i=0;
  394.          ngflag=TRUE;
  395.          while(guidword[i]!=0)
  396.           {if (guidword[i++]==42) ngflag=FALSE;}
  397.          if (ngflag==TRUE)
  398.           {i=0;
  399.           addr1=my_text;
  400.           do
  401.           (*addr1++)=textwin[i++];
  402.           while(textwin[i]!=0);
  403.           *addr1=0;
  404.           PrintIText(mywin->RPort, &my_intui_text, 135, topborder+12);}
  405.  
  406.         }          // else guidflag=true
  407.       }           // if ngflag=FALSE
  408.             break;  // case gadgetup
  409.  
  410.         case CLOSEWINDOW:
  411.             terminated = TRUE;
  412.             break;
  413.  
  414.         case IDCMPUPDATE:
  415.          if (FindTagItem (DTA_Sync, (struct TagItem *)imsg->IAddress))
  416.          RefreshGList ((struct Gadget *) dtobj, mywin, NULL, 1);
  417.          break;
  418.  
  419.         case REFRESHWINDOW:
  420.           if (ngflag==FALSE) PrintIText(mywin->RPort, &my_intui_text, 125, topborder+44);
  421.           else
  422.           {addr2=textwin;
  423.           if (wrongcnt>6) addr2=textgib;
  424.           addr1=my_text;
  425.           while(*addr2!=0) (*addr1++)=(*addr2++);
  426.           *addr1=0;
  427.           PrintIText(mywin->RPort, &my_intui_text, 135, topborder+12);
  428.           i=0;
  429.           addr1=my_text;
  430.           while(selword[i]!=0)
  431.           (*addr1++)=selword[i++];
  432.           while(i<20) { *addr1++=32; i++;}
  433.           PrintIText(mywin->RPort, &my_intui_text, 125, topborder+44);
  434.           }
  435.             GT_BeginRefresh(mywin);
  436.             GT_EndRefresh(mywin, TRUE);
  437.             break;
  438.         }
  439.     }
  440.     }
  441.     bail_out(0, NULL);
  442. return 0;
  443. }
  444.  
  445.  
  446. void bail_out( int code, STRPTR error)
  447. {   if (rdargs) FreeArgs (rdargs);
  448.  
  449.     if (dob) FreeDiskObject (dob);
  450.  
  451.     if (dtobj)
  452.     {RemoveDTObject (mywin,dtobj);
  453.      DisposeDTObject(dtobj);}
  454.  
  455.     if (DataTypesBase) CloseLibrary(DataTypesBase);
  456.  
  457.     if (mywin) CloseWindow(mywin);
  458.  
  459.     if (GadToolsBase)
  460.     {
  461.     FreeVisualInfo(vi);
  462.     FreeGadgets(glist);
  463.     CloseLibrary(GadToolsBase);
  464.     }
  465.  
  466.    if (wordsarray) FreeMem(wordsarray,lengthwords);
  467.  
  468.     if (mysc) UnlockPubScreen(NULL, mysc);
  469.  
  470.     if (font) CloseFont(font);
  471.  
  472.     if (DiskfontBase) CloseLibrary(DiskfontBase);
  473.  
  474.     if (IntuitionBase) CloseLibrary( ( struct Library *)IntuitionBase);
  475.  
  476.     if (GfxBase) CloseLibrary( ( struct Library *)GfxBase);
  477.  
  478.     if (IconBase) CloseLibrary( ( struct Library *)IconBase);
  479.  
  480.     if (UtilityBase) CloseLibrary( ( struct Library *)UtilityBase);
  481.  
  482.     if (error) printf("Error: %s\n", error);
  483.  
  484.     exit(code);
  485. }
  486.  
  487. struct Gadget *CreateAllGadgets( struct Gadget **glistptr, void *vi, UWORD topborder, APTR wordsarray)
  488. {
  489.     struct NewGadget ng;
  490.     struct Gadget *gad;
  491.     UBYTE *addr,*addr1,i,textgdarr[100],gadnum=0;
  492.     ULONG x,y,x0=135;
  493.  
  494.     addr=(UBYTE*)wordsarray;
  495.     while (*addr++!=0x0A)
  496.      gadnum++;
  497.  
  498.     addr=&textgdarr[0];
  499.     addr1=(UBYTE*)wordsarray;
  500.  
  501.      for(i=0;i<gadnum;i++)
  502.      {*addr++=*addr1++;
  503.       *addr++=0;}
  504.  
  505.     gad = CreateContext(glistptr);
  506.  
  507.     ng.ng_TextAttr = &Gibbet15;
  508.     ng.ng_VisualInfo = vi;
  509.  
  510.     ng.ng_LeftEdge = 245;
  511.     ng.ng_TopEdge = topborder+9;
  512.     ng.ng_Width = 40;
  513.     ng.ng_Height = 20;
  514.     ng.ng_GadgetText = "NEW";
  515.     ng.ng_GadgetID = 1;
  516.     ng.ng_Flags = 0;
  517.     gad = CreateGadget(BUTTON_KIND, gad, &ng,TAG_DONE);
  518.  
  519.     fistgad=gad;
  520.     i=1;
  521.     addr=&textgdarr[0];
  522.     if (gadnum>32) x0=125;
  523.      if (gadnum>36) x0=105;
  524.  
  525.     for (y=85;y<=145;y+=20)
  526.     {for (x=x0; x<=290; x+=20)
  527.      {ng.ng_LeftEdge = x;
  528.     ng.ng_TopEdge = y;
  529.     ng.ng_Width = 20;
  530.     ng.ng_Height = 20;
  531.     ng.ng_GadgetText = addr;
  532.     ng.ng_GadgetID = *addr;
  533.     ng.ng_Flags = 0;
  534.     gad = CreateGadget(BUTTON_KIND, gad, &ng,TAG_DONE);
  535.     i++;
  536.     if (i>gadnum) break;
  537.     addr+=2;
  538.      }
  539.     if (i>gadnum) break;
  540.     }
  541.  
  542.     return(gad);
  543. }
  544.  
  545.  
  546.  
  547. /*------------------------------------------------------------------------*/
  548.